/**
**作用：查看所有站点
**/
function showAllweb() {
	var url = "public/api.web.php/web/list";
    ajax("GET", url);
}
// 查看个人站点
// function showWeb() {
// 	var token = sessionStorage.getItem("token");
// 	var user_id = sessionStorage.getItem("user_id");
// 	var url = "public/api.web.php/web/mylist";
// 	var data = `user_id=${user_id}&token=${token}`;
// 	ajax("POST", url, data);
// }
document.getElementById("addweb").onclick = function() {
	addWeb();	
}
// 增加站点函数
function addWeb() {
	var url = "public/api.web.php/web/add";
	var web_name = document.getElementById("web_name").value;//只能是英文
	var web_info = document.getElementById("web_info").value;
	var web_author = document.getElementById("web_author").value;
	var web_status = "0";    //默认为公开
	var user_id =  sessionStorage.getItem("user_id");
	var group_id = "1";      //sessionStorage.getItem("group_id");
	var data = `web_name=${web_name}&web_info=${web_info}&web_author=${web_author}&web_status=${web_status}&user_id=${user_id}&group_id=${group_id}`;
	ajax("POST", url, data);
}
/**
 *
 方法说明：ajax封装函数
 *
 @method ajax()
 *
 @param {string}   type   提交的类型
 @param {string}   url    提交到的URL地址
 @param {string}   data   POST提交时带的参数
 *
 @return 
 */
function ajax(type, url, data) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open(type, url);
	if(type == "POST") {
	    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    }
    if(type == "POST") {
	    xmlhttp.send(data);
	}else {
		xmlhttp.send();
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState===4) {
			if (xmlhttp.status===200) {
				var txt = JSON.parse(xmlhttp.responseText);
				prosperity(txt);
			} else {
				alert("网页发生错误：" + xmlhttp.status);
			}
		} 
	}
}
function prosperity(txt) {
	console.log(txt);
}
// 校验表单不能为空
function checkFrom() {
	var form = document.getElementById("js_webform");
	var formLen = form.length;
	for(var i = 0; i < formLen-1; i++) {
		if(form[i].value == "") {
			alert(form[i] + "不能为空");
			return false;
		}
	}
}